import { type FormInstance } from 'antd';

import { FunctionTypeList, objToArray, ReadWriteTypeList } from '@/constants';

import DataTypeComponent from './DataTypeComponent';
import ParamListComponent from './ParamListComponent';

const ModelAddForm = (props: { form: FormInstance; readonly?: boolean }) => {
  const { form, readonly = false } = props;
  // 数据类型
  const _dataType = AForm.useWatch('dataType', form);
  // 功能类型
  const _type = AForm.useWatch('type', form);

  return (
    <ARow gutter={8}>
      <ACol span={24}>
        <AForm.Item
          name="name"
          label="功能名称"
          rules={[{ required: true, message: '请输入功能名称' }]}
        >
          <AInput placeholder="请输入功能名称" allowClear disabled={readonly}></AInput>
        </AForm.Item>
      </ACol>
      <ACol span={24}>
        <AForm.Item
          name="identifier"
          label="标识符"
          rules={[{ required: true, message: '请输入标识符' }]}
        >
          <AInput placeholder="请输入标识符" allowClear disabled={readonly}></AInput>
        </AForm.Item>
      </ACol>
      <ACol span={24}>
        <AForm.Item
          name="type"
          label="功能类型"
          rules={[{ required: true, message: '请选择功能类型' }]}
        >
          <ASelect options={objToArray(FunctionTypeList)} disabled={readonly}></ASelect>
        </AForm.Item>
      </ACol>
      {_type === 'services' && (
        <ACol span={24}>
          <AForm.Item
            name="callType"
            label="调用方式"
            rules={[{ required: false, message: '请选择调用方式' }]}
          >
            <ASelect disabled={readonly}>
              <ASelect.Option value="sync">同步</ASelect.Option>
              <ASelect.Option value="async" disabled={true}>
                异步
              </ASelect.Option>
            </ASelect>
          </AForm.Item>
        </ACol>
      )}

      {/* 数据类型组件 - 属性类型时显示 */}
      {_type === 'properties' && (
        <DataTypeComponent form={form} dataType={_dataType} readonly={readonly} />
      )}

      {/* 参数列表组件 - 事件类型时显示 */}
      {_type === 'events' && (
        <ParamListComponent form={form} fieldName="paramList" readonly={readonly} />
      )}
      {/* 参数列表组件 - 服务类型时显示 */}
      {_type === 'services' && (
        <ParamListComponent
          form={form}
          fieldName="inputData"
          title="输入参数(最多支持50个参数)"
          readonly={readonly}
        />
      )}
      {/* 参数列表组件 - 服务类型时显示 */}
      {_type === 'services' && (
        <ParamListComponent form={form} fieldName="outputData" readonly={readonly} />
      )}

      {_type === 'properties' && (
        <ACol span={24}>
          <AForm.Item
            name="accessMode"
            label="读写类型"
            rules={[{ required: true, message: '请选择读写类型' }]}
          >
            <ASelect options={objToArray(ReadWriteTypeList)} disabled={readonly}></ASelect>
          </AForm.Item>
        </ACol>
      )}
      <ACol span={24}>
        <AForm.Item name="remark" label="备注" rules={[{ required: false, message: '请输入备注' }]}>
          <AInput.TextArea
            placeholder="请输入备注"
            allowClear
            disabled={readonly}
          ></AInput.TextArea>
        </AForm.Item>
      </ACol>
    </ARow>
  );
};

export default ModelAddForm;
